package com.group3.admin.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class LoginServlet
 * 
 * 登录Servlet ： 管理员身份认证 authentication
 * 	1. 管理员的账号密码均为 admin           admin/admin
 * 	2.  设置session中的username 
 * 3.  结合AuthFilter 接入控制
 */
//@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
//		request.getSession().setAttribute("type", "ALL");
		request.getSession().setAttribute("loginToken", "admin");
		if ("admin".equals(username) && "admin".equals(password)) {
			System.out.println("LoginServlet : admin logged in !");
			request.getSession().setAttribute("username", "admin");
			response.sendRedirect("mlist.html");
//			request.getRequestDispatcher("MListServlet").forward(request, response);		
		} else {
			System.out.println("LoginServlet : Login in failed!");
			response.sendRedirect("login.jsp");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
